package com.modele.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.modele.dao.DAO;
import com.modele.entity.Restaurant;
import com.modele.entity.TypeRestaurant;


public class RestaurantDAO extends DAO<Restaurant>{

	public RestaurantDAO(Connection connexion) {
		super(connexion);
	}

	@Override
	public boolean create(Restaurant obj) {
		try {
			PreparedStatement statement = connexion.prepareStatement("INSERT INTO restaurant " +
				"(nom, type, ville, num, rue, cp, description,tel,site) " +
				"VALUES (?,?,?,?,?,?,?,?,?)");
			statement.setString(1, obj.getNom());
			statement.setInt(2,obj.getTypeRestaurant().getId());
			statement.setString(3, obj.getVille());
			statement.setString(4, obj.getNum());
			statement.setString(5, obj.getRue());
			statement.setString(6, obj.getCp());
			statement.setString(7, obj.getDescription());
			statement.setString(8, obj.getTel());
			statement.setString(9, obj.getSite());
			return statement.executeUpdate() == 1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
	}
	public int findIdTypeByName(String nom){
		int id=0;
		try {
			PreparedStatement statement = connexion.prepareStatement("SELECT id FROM typeRestaurant WHERE type = '"+nom+"'");
			ResultSet rs = statement.executeQuery();
			rs.next();
			return rs.getInt("id");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return id;
	}
	public String findTypeById(int id){
		
		try {
			PreparedStatement statement = connexion.prepareStatement("SELECT type FROM typeRestaurant WHERE id = ("+id+")");
			ResultSet rs = statement.executeQuery();
			rs.next();
			return rs.getString("type");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "" ;
	}


	@Override
	public boolean update(Restaurant obj) {
		try {
			PreparedStatement statement = connexion.prepareStatement("UPDATE restaurant SET " +
				"`nom`='"+obj.getNom()+
				"',`type`="+obj.getTypeRestaurant().getId()+
				",`ville`='"+obj.getVille()+
				"',`num`='"+obj.getNum()+
				"',`rue`='"+obj.getRue()+
				"',`cp`='"+obj.getCp()+
				"',`description`='"+obj.getDescription()+
				"',`tel`='"+obj.getTel()+
				"',`site`='"+obj.getSite()+"'" +
				" WHERE id = "+obj.getId());
				
			return statement.executeUpdate() == 1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public List<Restaurant> findAll() {
		List<Restaurant> list = new ArrayList<Restaurant>();
		try {
			PreparedStatement statement = connexion.prepareStatement("SELECT * FROM restaurant ");
			ResultSet rs = statement.executeQuery();
			while (rs.next()){
				list.add(new Restaurant(rs.getString("nom"),
							new TypeRestaurant(rs.getInt("type"),findTypeById(rs.getInt("type"))),
							rs.getString("ville"),
							rs.getString("num"), 
							rs.getString("rue"), 
							rs.getString("cp"),
							rs.getString("description"),
							rs.getString("tel"),
							rs.getString("site"))	
				);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Restaurant> findByType(String type){
		List<Restaurant> list = new ArrayList<Restaurant>();
		try {
			int idType=this.findIdTypeByName(type);
			TypeRestaurant typeRestaurant = new TypeRestaurant(idType, type);
			PreparedStatement statement = connexion.prepareStatement("SELECT * FROM restaurant WHERE type ="+idType);
			ResultSet rs = statement.executeQuery();
			while (rs.next()){
				list.add(new Restaurant(rs.getString("nom"),
							new TypeRestaurant(rs.getInt("type"),findTypeById(rs.getInt("type"))),
							rs.getString("ville"),
							rs.getString("num"), 
							rs.getString("rue"), 
							rs.getString("cp"),
							rs.getString("description"),
							rs.getString("tel"),
							rs.getString("site"))	
				);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public Restaurant findByValue(Object obj) {
		Restaurant resto = null;
		
		Integer idResto = (Integer) obj;
		try 
		{
			PreparedStatement statement = connexion.prepareStatement("SELECT * FROM restaurant WHERE id = ? ");
			statement.setInt(1, idResto);
			
			if(statement.execute())
			{
				ResultSet result = statement.getResultSet();
				
				if(result.next())
				{				
					resto = new Restaurant();
					
					
					resto.setId(idResto);
					resto.setNom(result.getString(2));
					resto.setIdTypeRestaurant(result.getString(3));
					//resto.set(result.getString(3));
					resto.setVille(result.getString(4));
					resto.setNum(result.getString(5));
					resto.setRue(result.getString(6));
					resto.setCp(result.getString(7));
					resto.setDescription(result.getString(8));
					resto.setTel(result.getString(9));
					resto.setSite(result.getString(10));
					resto.setIdCarte(result.getInt(11));
				}
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return resto;
	}
	
	@Override
	public boolean isPresent(Object obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Restaurant find(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Restaurant findByName(String name) {
		try {

			PreparedStatement statement = connexion.prepareStatement("SELECT * FROM restaurant WHERE nom ='"+name+"'");
			ResultSet rs = statement.executeQuery();
			rs.next();
			Restaurant resto = new Restaurant(
							rs.getInt("id"),
							rs.getString("nom"),
							new TypeRestaurant(rs.getInt("type"),findTypeById(rs.getInt("type"))),
							rs.getString("ville"),
							rs.getString("num"), 
							rs.getString("rue"), 
							rs.getString("cp"),
							rs.getString("description"),
							rs.getString("tel"),
							rs.getString("site"),
							rs.getInt("idCarte"));
			
			return resto;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public boolean delete(Restaurant resto) {
			try {
				PreparedStatement statement = connexion.prepareStatement("DELETE FROM `restaurant` WHERE `restaurant`.`id`="+resto.getId());
				statement.execute();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return false;
	}
}
